package com.example.ldachu.testdownload.http;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * @author zxKueen on 2017-12-22 8:49
 *         Email: 4994766@qq.com
 */

public class RetrofitFactory {
    private static volatile RetrofitFactory instance;
    private final Retrofit mRetrofit;

    private RetrofitFactory() {
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .build();
        mRetrofit = new Retrofit.Builder()
                .client(okHttpClient)
                .addConverterFactory(GsonConverterFactory.create())//添加gson转换器
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())//添加rxjava转换器
                .baseUrl(NetConfig.url)
                .build();
    }


    public RetrofitFactory setUrl(String url){
        mRetrofit.newBuilder()
                .baseUrl(url)
                .build();


        return this;
    }


    public static RetrofitFactory getInstance() {
        if (instance == null) {
            synchronized (RetrofitFactory.class) {
                if (instance == null) {
                    instance = new RetrofitFactory();
                }
            }
        }
        return instance;
    }

    public DownloadService getService(){
        return mRetrofit.create(DownloadService.class);
    }
}
